<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>
<script>
import BScroll from 'better-scroll'
export default {
  name:'Scroll',
  props:{
    probeType:{
      type:Number,
      default: 1
    },
    // 上拉加载更多
    pullUpLoad:{
      type:Boolean,
      default:false
    },
    observeImage:{
      type:Boolean,
      default:false
    },

  },
  data() {
    return {
       scroll:null,
       message:'我是scroll'
    }
  },
  mounted() {
     this.scroll = new BScroll(this.$refs.wrapper,{
       click:true,
      //  mouseWheel:true,
      observeDOM:true,
      // tap:true,
      // 用来控制滑动的时候计算位置x和y
      probeType:this.probeType,
      pullUpLoad:this.pullUpLoad,
      // 等到页面加载完后刷新content的高度
      observeImage:this.observeImage
     })
    //  this.scroll.scrollTo(0,0)
    if(this.probeType === 2 || this.probeType === 3){
       this.scroll.on('scroll',(position) =>{
       this.$emit("scroll",position)
      })
    }
    //  监听下拉加载更多
    if(this.pullUpLoad) {
      this.scroll.on('pullingUp',() =>{
        this.$emit('pullingUp')
      })
    }
  },
  watch: {
     data() {
       setTimeout(this.refresh,20)
     }
  },
  methods: {
     scrollTo(x,y,time = 500) {
        this.scroll && this.scroll.scrollTo(0,0,time)
     },
     finishPullUp(){
      this.scroll && this.scroll.finishPullUp()
     },
     refresh(){
      //  console.log('---')
       this.scroll && this.scroll.refresh()
     },
     getScrollY(){
       return this.scroll ? this.scroll.y :0
     }
  },

}
</script>
<style scoped>

</style>
